﻿using System;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace Financial
{

    public class Financeira
    {
        public Financeira()
        {
            //Criar socket do lado do servidor
            Socket socketServidor = new Socket(AddressFamily.InterNetwork,
                                               SocketType.Stream,
                                               ProtocolType.Tcp);

            //Criar EndPoint e admitir qualquer IP
            //Ver camada de transporte
            IPEndPoint ie = new IPEndPoint(IPAddress.Any, 7000);

            //Associar socket ao IPEndPoint local
            socketServidor.Bind(ie);
            //Socket à escuta, no máximo 10 ligações
            socketServidor.Listen(10);

            Console.WriteLine("`•.,¸¸,.•´¯ SERVIDOR FINANCEIRO ¯`•.,¸¸,.•´\n");
            Console.WriteLine("Espera de pedidos....");

            while (true)
            {
                ConnectionThread novaConexao = new ConnectionThread();

                novaConexao.threadClient = socketServidor.Accept();

                Thread novoThread = new Thread(new ThreadStart(() => novaConexao.HandleConnection()));

                novoThread.Start();
                if (novaConexao.connections >= 10 || novaConexao.sessaoFechada)
                {
                    break;
                }
            }
            socketServidor.Close();      
        }
  	}
}

